package com.example.toutiao.view

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.example.toutiao.R
import com.example.toutiao.utils.ImageLoaderUtils
import kotlinx.android.synthetic.main.item_user_info_view.view.*

class UserInfoItemView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    private var leftTitle:String?=""
    private var isShowImage:Boolean=false
    private var view:View?=null
    private val userType:TypedArray=context.obtainStyledAttributes(attrs, R.styleable.UserItem)
    private var mUserOnClickListener:UserOnClickListener?=null

    init {
        leftTitle= userType.getString(R.styleable.UserItem_user_leftTitle)
        isShowImage=userType.getBoolean(R.styleable.UserItem_user_imageShow,false)
        userType.recycle()
        initView()

    }

    private fun initView() {

        view=View.inflate(context,R.layout.item_user_info_view,this)
        view!!.tv_left_title.text=leftTitle
        // 接口回调
        view!!.rl_item.setOnClickListener {
            mUserOnClickListener!!.userOnClick(it)
        }
        isShow()
    }

    private fun isShow() {
        view!!.iv_photo.isVisible = isShowImage
    }

    fun setImageUrl(url:String){
        ImageLoaderUtils.loadCircleImage(context,url,view!!.iv_photo)
    }

    fun setTextRightColor(colorIds:Int){
        view!!.tv_content.setTextColor(ContextCompat.getColor(context,colorIds))
        view!!.tv_content.isVisible=true
    }

    fun setRightText(content:String){
        view!!.tv_content.text=content
        view!!.tv_content.isVisible=true
    }
    fun setUserOnClickListener(mUserOnClickListener: UserOnClickListener){
        this.mUserOnClickListener=mUserOnClickListener
    }
    interface  UserOnClickListener{
      fun  userOnClick(view:View)
    }
}